package com.raspositos.guitarfrik.android;

import android.provider.BaseColumns;

public class TrackSection implements BaseColumns, Comparable<TrackSection> {

	public static final String TRACK_ID = "track_id";
	public static final String OFFSET = "offset";
	public static final String DURATION = "duration";
	public static final String NAME = "name";
	public static final String INDEX = "name_index";
	public enum Sections {
		Intro ("intro"),
		Verse("verse"),
		PreChorus("pre-chorus"),
		Chorus("chorus"),
		Bridge("bridge"),
		Solo("solo"),
		Outro("outro"),
		Break("break"),
		Groove("groove"),
		Collision("collision"),
		VocalSolo("vocal solo"),
		AdLib("ad lib"),
		Durchkomponiert("durchkomponiert"),
		Coda("coda"),
		Unknown("");
		private String name;
		Sections (String name){
			this.name = name;
		}
		
		private static Sections fromName(String name){
			Sections sec = Unknown;
			name = (name + "").toLowerCase();
			for (int i = 0; i < Sections.values().length; i++) {
				Sections test = Sections.values()[i];
				if (name.startsWith(test.name)){
					sec = test;
					break;
				}
			}
			return sec;
		}
	}
	
	private int offset;
	private int duration;
	private String index = "";
	private String name;
	private int trackid;
	private long id;

	public TrackSection(int trackid, int offset, int duration, String name) {
		this.trackid = trackid;
		this.offset = offset;
		this.duration = duration;
		this.name = name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TrackSection other = (TrackSection) obj;
		if (duration != other.duration)
			return false;
		if (index == null) {
			if (other.index != null)
				return false;
		} else if (!index.equals(other.index))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (offset != other.offset)
			return false;
		if (trackid != other.trackid)
			return false;
		return true;
	}

	public final int getDuration() {
		return duration;
	}

	public long getId() {
		return id;
	}

	public final String getIndex() {
		return index;
	}

	public final String getName() {
		return name;
	}

	public final int getOffset() {
		return offset;
	}

	/**
	 * @return the trackid
	 */
	public final int getTrackId() {
		return trackid;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + duration;
		result = prime * result + ((index == null) ? 0 : index.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + offset;
		result = prime * result + trackid;
		return result;
	}

	public void setId(long id) {
		this.id = id;
	}

	public final void setIndex(String index) {
		this.index = index;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("TrackSection [duration=").append(duration).append(
				", name=").append(name).append(", offset=").append(offset)
				.append("]");
		return builder.toString();
	}

	public int compareTo(TrackSection ts) {
		Sections thisSect = Sections.fromName(this.name);
		Sections otSect = Sections.fromName(ts.name);
		
		return thisSect.compareTo(otSect);
	}

}
